package it.azienda.managerworkstandard.view;

import it.azienda.framework.utilita.Utilita;
import it.azienda.managerworkstandard.modello.BeanProdotto;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import org.apache.myfaces.component.html.ext.HtmlInputHidden;
import org.apache.myfaces.custom.tabbedpane.HtmlPanelTab;
import org.apache.myfaces.custom.tabbedpane.HtmlPanelTabbedPane;
import org.apache.myfaces.custom.tabbedpane.TabChangeEvent;
import org.apache.myfaces.custom.tabbedpane.TabChangeListener;

public class TabProdotto implements TabChangeListener {
    
    private static final long serialVersionUID = 1L;
    int selectedTabIndex;
    
    public TabProdotto() {
        super();
    }
    
    public int getSelectedTabIndex() {
        return selectedTabIndex;
    }
    public void setSelectedTabIndex(int selectedTabIndex) {
        this.selectedTabIndex = selectedTabIndex;
    }
    
    public void processTabChange(TabChangeEvent event)throws AbortProcessingException{
        event.getNewTabIndex();
        this.selectedTabIndex = event.getNewTabIndex();
        FacesContext ctx = FacesContext.getCurrentInstance();
        if(event.getOldTabIndex()!=event.getNewTabIndex()){
            HtmlPanelTab panelTab1=null;
            
            if(this.getSelectedTabIndex()==0){
                ctx = FacesContext.getCurrentInstance();
                BeanProdotto beanProdotto=(BeanProdotto) ctx.getApplication().getVariableResolver().resolveVariable(ctx, "beanProdotto");
                Utilita.getRequest().setAttribute("beanProdotto",beanProdotto);
                BeanProdotto Prodotto=(BeanProdotto)Utilita.getRequest().getAttribute("beanProdotto");
                return;
            }else{
                HtmlPanelTabbedPane tabbedPane =(HtmlPanelTabbedPane) ctx.getViewRoot().findComponent("panelTabbedPane");
                panelTab1=(HtmlPanelTab)tabbedPane.findComponent("tab0");
                HtmlForm form1=(HtmlForm)panelTab1.findComponent("formLookup");
                HtmlInputHidden idProdotto=(HtmlInputHidden)form1.findComponent("idProdotto");
                String gg="";
                if(idProdotto.getValue()!=null){
                    gg=idProdotto.getValue().toString();
                }else{
                    return;
                }
                DettaglioProdotto dettaglio=new DettaglioProdotto();
                Integer tab=new Integer(selectedTabIndex);
                dettaglio.caricaStoricoProdotto(new Integer(gg), tab);
                return;
            }
        }
    }
    
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }
}